/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Canadian Weather.
 *
 * The Initial Developer of the Original Code is
 *   George Bradt.
 *
 * Portions created by the Initial Developer are Copyright (C) 2011
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   George Bradt.
 *
 * Credits:
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

var textToXml = require('textToXml')
var parseXml = require('parseXml');
var data = require('sdk/self').data;

var degreeSymbol = '\u00B0';
var options = {
        language: 'e',  // e=English, f=French
        toolbarDisplay: {showTemperature: true,
                         showWind: true,
                         showWindChill: true,
                         showHumidity: true,
                         showHumidex: true,
                         showCondition: true,
                         showConditionIcon: true},
        toolbarDisplayEnabled: true,
        conditionsPanelEnabled: true,
        warningPanelEnabled: true
    };

// A typical site data file.
var xmlObjectComplete = new textToXml.convert(data.load('xmlTest/siteDataComplete.txt'));
var parsedDataComplete = parseXml.parse(xmlObjectComplete, options);

// A partial site data file to test calm wind.
var xmlObjectNoWind = new textToXml.convert(data.load('xmlTest/siteDataNoWind.txt'));
var parsedDataNoWind = parseXml.parse(xmlObjectNoWind, options);

// A partial site data file to test wind gusts.
var xmlObjectWindGusts = new textToXml.convert(data.load('xmlTest/siteDataWindGusts.txt'));
var parsedDataWindGusts = parseXml.parse(xmlObjectWindGusts, options);

// A partial site data file to test wind chill.
var xmlObjectWindChill = new textToXml.convert(data.load('xmlTest/siteDataWindChill.txt'));
var parsedDataWindChill = parseXml.parse(xmlObjectWindChill, options);

// A partial site data file to test optional humidex.
var xmlObjectHumidex = new textToXml.convert(data.load('xmlTest/siteDataHumidex.txt'));
var parsedDataHumidex = parseXml.parse(xmlObjectHumidex, options);

// A partial site data file to test warnings.
var xmlObjectWarnings = new textToXml.convert(data.load('xmlTest/siteDataWarnings.txt'));
var parsedDataWarnings = parseXml.parse(xmlObjectWarnings, options);

exports['testParseXmlComplete'] = function(assert) {
    assert.ok(parsedDataComplete !== undefined, 'parse xml complete is ok');
}

exports['testParseXmlNoWind'] = function(assert) {
    assert.ok(parsedDataNoWind !== undefined, 'parse xml no wind is ok');
}

exports['testParseXmlWindGusts'] = function(assert) {
    assert.ok(parsedDataWindGusts !== undefined, 'parse xml wind gusts is ok');
}

exports['testParseXmlWindChill'] = function(assert) {
    assert.ok(parsedDataWindChill !== undefined, 'parse xml wind chill is ok');
}

exports['testParseXmlHumidex'] = function(assert) {
    assert.ok(parsedDataHumidex !== undefined, 'parse xml humidex is ok');
}

exports['testParseXmlWarnings'] = function(assert) {
    assert.ok(parsedDataWarnings !== undefined, 'parse xml warnings is ok');
}

exports['testBarDisplayString'] = function(assert) {
    let str = '15' + degreeSymbol + 'C  93 %  Cloudy  SW 13 km/h';
    assert.ok(parsedDataComplete.barDisplayString === str, 'bar display string is ok');
}

exports['testLocationName'] = function(assert) {
    let str = 'Windsor';
    assert.ok(parsedDataComplete.location.name === str, 'location name is ok');
}

exports['testLocationRegion'] = function(assert) {
    let str = 'Windsor - Leamington - Essex County';
    assert.ok(parsedDataComplete.location.region === str, 'location region is ok');
}

exports['testLocationStation'] = function(assert) {
    let str = 'Windsor Airport';
    assert.ok(parsedDataComplete.location.station === str, 'location station is ok');
}
exports['testCurrentConditionsDate'] = function(assert) {
    let str = '10:33 PM EDT on May 25';
    assert.ok(parsedDataComplete.currentConditions.date === str, 'current conditions date is ok');
}

exports['testCurrentConditionsCondition'] = function(assert) {
    let str = 'Cloudy';
    assert.ok(parsedDataComplete.currentConditions.condition === str, 'current condition is ok');
}

exports['testCurrentConditionsTemperature'] = function(assert) {
    let str = '15.4' + degreeSymbol + 'C';
    assert.ok(parsedDataComplete.currentConditions.temperature === str, 'current temperature is ok');
}

exports['testCurrentConditionsDewpoint'] = function(assert) {
    let str = '14.3' + degreeSymbol + 'C';
    assert.ok(parsedDataComplete.currentConditions.dewpoint === str, 'current dewpoint is ok');
}

exports['testCurrentConditionsWindChill'] = function(assert) {
    let str = 'N/A';
    assert.ok(parsedDataComplete.currentConditions.windChill === str, 'current wind chill is ok');
}

exports['testCurrentConditionsHumidex'] = function(assert) {
    let str = 'N/A';
    assert.ok(parsedDataComplete.currentConditions.humidex === str, 'current humidex is ok');
}

exports['testCurrentConditionsPressure'] = function(assert) {
    let str = '100.5 kPa falling';
    assert.ok(parsedDataComplete.currentConditions.pressure === str, 'current pressure is ok');
}

exports['testCurrentConditionsVisibility'] = function(assert) {
    let str = '24.1 km';
    assert.ok(parsedDataComplete.currentConditions.visibility === str, 'current visibility is ok');
}

exports['testCurrentConditionsRelativeHumidity'] = function(assert) {
    let str = '93 %';
    assert.ok(parsedDataComplete.currentConditions.relativeHumidity === str, 'current relative humidity is ok');
}

exports['testCurrentConditionsWind'] = function(assert) {
    let str = 'SW 13 km/h';
    assert.ok(parsedDataComplete.currentConditions.wind === str, 'current wind is ok');
}

exports['testForecastDate'] = function(assert) {
    let str = '10:31 PM EDT on May 25';
    assert.ok(parsedDataComplete.forecasts.date === str, 'forecast date is ok');
}

exports['testForecastPeriod'] = function(assert) {
    let str = 'Wednesday night';
    assert.ok(parsedDataComplete.forecasts.forecast[0].period === str, 'forecast period is ok');
}

exports['testForecastSummary'] = function(assert) {
    let str = 'Showers at times heavy with thunderstorms. Amount 20 to 30 mm. Temperature steady near 17.';
    assert.ok(parsedDataComplete.forecasts.forecast[0].summary === str, 'forecast is ok');
}

// Calm wind.
exports['testParseXmlNoWind'] = function(assert) {
    let str = 'Calm';
    assert.ok(parsedDataNoWind.currentConditions.wind === str, 'no wind is ok');
}

// Wind gusts.
exports['testParseXmlWindGusts'] = function(assert) {
    let str = 'E 22 gust 32 km/h';
    assert.ok(parsedDataWindGusts.currentConditions.wind === str, 'wind gusts is ok');
}

// Wind chill.
exports['testParseXmlWindChill'] = function(assert) {
    let str = '-8' + degreeSymbol;
    assert.ok(parsedDataWindChill.currentConditions.windChill === str, 'wind chill is ok');
}

// Humidex.
exports['testParseXmlHumidex'] = function(assert) {
    let str = '35' + degreeSymbol;
    assert.ok(parsedDataHumidex.currentConditions.humidex === str, 'humidex is ok');
}

// Warnings URL.
exports['testParseXmlWarningsUrl'] = function(assert) {
    let url = 'http://weatheroffice.ec.gc.ca/warnings/report_e.html?qc66';
    assert.ok(parsedDataWarnings.warnings[0] === url, 'url is ok');
}

// Warnings type. Use first event.
exports['testParseXmlWarningsType'] = function(assert) {
    let str = 'Watch';
    assert.ok(parsedDataWarnings.warnings[1].type === str, 'warning type is ok');
}

// Warnings priority. Use first event.
exports['testParseXmlWarningsPriority'] = function(assert) {
    let str = 'Low';
    assert.ok(parsedDataWarnings.warnings[1].priority === str, 'warning priority is ok');
}

// Warnings description. Use first event.
exports['testParseXmlWarningsDescription'] = function(assert) {
    let str = 'SEVERE THUNDERSTORM WATCH  IN EFFECT';
    assert.ok(parsedDataWarnings.warnings[1].description === str, 'warning description is ok');
}

// Warnings date. Use first event.
// Ensure this runs last (alphabetical order, 'testZZ...') to log a banner
// to seperate from textToXml tests.
exports['testZZParseXmlWarningsDate'] = function(assert) {
    let str = '3:25 PM EDT on May 26';
    assert.ok(parsedDataWarnings.warnings[1].date === str, 'warning date is ok');
    console.log('');
    console.log('************************************************************');
    console.log('');
}

require('sdk/test').run(exports);
